﻿using Egg;
using FolderSync.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FolderSync
{
    public partial class FrmEdit : Form
    {
        /// <summary>
        /// 同步信息
        /// </summary>
        public SyncInfo? SyncInfo { get; private set; } = null;

        public FrmEdit()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置为编辑模式
        /// </summary>
        /// <param name="syncInfo"></param>
        public void SetEditMode(SyncInfo syncInfo)
        {
            this.SyncInfo = new SyncInfo()
            {
                Folder = syncInfo.Folder,
                Id = syncInfo.Id,
                LastSyncTime = syncInfo.LastSyncTime,
                Name = syncInfo.Name,
                SourceFolder = syncInfo.SourceFolder,
            };
            this.textBox1.Text = syncInfo.Folder;
            this.textBox2.Text = syncInfo.SourceFolder;
            this.textBox3.Text = syncInfo.Name;
            this.Text = $"编辑项目 - ID:{syncInfo.Id}";
        }

        /// <summary>
        /// 设置为编辑模式
        /// </summary>
        /// <param name="syncInfo"></param>
        public void SetAddMode()
        {
            this.SyncInfo = new SyncInfo();
            this.Text = $"添加项目";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog f = new FolderBrowserDialog())
            {
                f.SelectedPath = this.textBox2.Text.Trim();
                if (f.ShowDialog() == DialogResult.OK)
                {
                    this.textBox2.Text = f.SelectedPath;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.SyncInfo is null) return;
            // 名i成
            var name = this.textBox3.Text.Trim();
            if (name.IsEmpty()) { MessageBox.Show("名称不能为空"); return; }
            this.SyncInfo.Name = name;
            // 目录
            var folder = this.textBox1.Text.Trim();
            if (folder.IsEmpty()) { MessageBox.Show("目录不能为空"); return; }
            if (!folder.StartsWith("/")) { MessageBox.Show("目录必须是'/'开头"); return; }
            this.SyncInfo.Folder = folder;
            // 目标目录
            var sourceFolder = this.textBox2.Text.Trim();
            if (sourceFolder.IsEmpty()) { MessageBox.Show("目标目录不能为空"); return; }
            this.SyncInfo.SourceFolder = sourceFolder;
            // 最后更新时间
            this.SyncInfo.LastSyncTime = null;
            // 返回结果标志
            this.DialogResult = DialogResult.OK;
        }
    }
}
